#  Copyright (c) 2014-present, Facebook, Inc.
#  All rights reserved.
#
#  This source code is licensed as defined on the LICENSE file found in the
#  root directory of this source tree.

load("//tools/build_defs/oss/osquery:cxx.bzl", "osquery_cxx_library")
load("//tools/build_defs/oss/osquery:native.bzl", "osquery_target")
load("//tools/build_defs/oss/osquery:platforms.bzl", "FREEBSD", "LINUX", "MACOSX", "POSIX", "WINDOWS")
load("//tools/build_defs/oss/osquery:third_party.bzl", "osquery_tp_target")

osquery_cxx_library(
    name = "system_table",
    srcs = [
        "cpuid.cpp",
        "hash.cpp",
        "python_packages.cpp",
        "ssh_configs.cpp",
        "ssh_keys.cpp",
        "system_utils.cpp",
        "uptime.cpp",
    ],
    header_namespace = "osquery/tables/system",
    exported_headers = [
        "efi_misc.h",
        "intel_me.hpp",
        "smbios_utils.h",
        "system_utils.h",
        "user_groups.h",
    ],
    exported_platform_headers = [
        (
            POSIX,
            [
                "posix/apt_sources.h",
                "posix/known_hosts.h",
                "posix/shell_history.h",
                "posix/sudoers.h",
                "posix/sysctl_utils.h",
                "posix/last.h",
            ],
        ),
        (
            LINUX,
            [
                "linux/md_tables.h",
                "linux/pci_devices.h",
                "linux/smbios_utils.h",
            ],
        ),
        (
            MACOSX,
            [
                "darwin/asl_utils.h",
                "darwin/firewall.h",
                "darwin/keychain.h",
                "darwin/packages.h",
                "darwin/smbios_utils.h",
            ],
        ),
        (
            FREEBSD,
            [
                "freebsd/procstat.h",
            ],
        ),
        (
            WINDOWS,
            [
                "windows/registry.h",
                "windows/certificates.h",
                "windows/users.h",
            ],
        ),
    ],
    exported_post_platform_linker_flags = [
        (
            FREEBSD,
            [
                "-laugeas",
                "-lmagic",
            ],
        ),
    ],
    platform_compiler_flags = [
        (
            MACOSX,
            [
                "-Wno-c99-extensions",
                "-Wno-undeclared-selector",
            ],
        ),
    ],
    platform_deps = [
        (
            LINUX,
            [
                osquery_tp_target("libdevmapper"),
                osquery_tp_target("libdpkg"),
                osquery_tp_target("libelfin", "elf"),
                osquery_tp_target("libcryptsetup"),
                osquery_tp_target("librpm"),
                osquery_tp_target("popt"),
            ],
        ),
        (
            MACOSX,
            [
                osquery_tp_target("openssl", "crypto"),
            ],
        ),
    ],
    platform_srcs = [
        (
            POSIX,
            [
                "posix/apt_sources.cpp",
                "posix/augeas.cpp",
                "posix/authorized_keys.cpp",
                "posix/crontab.cpp",
                "posix/known_hosts.cpp",
                "posix/last.cpp",
                "posix/load_average.cpp",
                "posix/logged_in_users.cpp",
                "posix/magic.cpp",
                "posix/shell_history.cpp",
                "posix/smbios_utils.cpp",
                "posix/sudoers.cpp",
                "posix/suid_bin.cpp",
                "posix/system_controls.cpp",
                "posix/ulimit_info.cpp",
                "posix/yum_sources.cpp",
            ],
        ),
        (
            LINUX,
            [
                "freenux/cpu_time.cpp",
                "linux/acpi_tables.cpp",
                "linux/apparmor_profiles.cpp",
                "linux/block_devices.cpp",
                "linux/deb_packages.cpp",
                "linux/disk_encryption.cpp",
                "linux/elf_info.cpp",
                "linux/groups.cpp",
                "linux/intel_me.cpp",
                "linux/kernel_info.cpp",
                "linux/kernel_modules.cpp",
                "linux/md_tables.cpp",
                "linux/memory_info.cpp",
                "linux/memory_map.cpp",
                "linux/model_specific_register.cpp",
                "linux/mounts.cpp",
                "linux/npm_packages.cpp",
                "linux/os_version.cpp",
                "linux/pci_devices.cpp",
                "linux/portage.cpp",
                "linux/process_open_files.cpp",
                "linux/processes.cpp",
                "linux/rpm_packages.cpp",
                "linux/selinux_settings.cpp",
                "linux/shadow.cpp",
                "linux/shared_memory.cpp",
                "linux/smbios_tables.cpp",
                "linux/sysctl_utils.cpp",
                "linux/system_info.cpp",
                "linux/usb_devices.cpp",
                "linux/user_groups.cpp",
                "linux/users.cpp",
            ],
        ),
        (
            MACOSX,
            [
                "darwin/account_policy_data.mm",
                "darwin/acpi_tables.cpp",
                "darwin/ad_config.cpp",
                "darwin/apps.mm",
                "darwin/asl.cpp",
                "darwin/asl_utils.cpp",
                "darwin/authorizations.mm",
                "darwin/battery.mm",
                "darwin/block_devices.cpp",
                "darwin/certificates.mm",
                "darwin/cpu_time.cpp",
                "darwin/crashes.cpp",
                "darwin/cups_destinations.cpp",
                "darwin/cups_jobs.cpp",
                "darwin/disk_encryption.mm",
                "darwin/event_taps.mm",
                "darwin/extended_attributes.cpp",
                "darwin/firewall.cpp",
                "darwin/gatekeeper.cpp",
                "darwin/homebrew_packages.cpp",
                "darwin/ibridge.cpp",
                "darwin/iokit_registry.cpp",
                "darwin/kernel_extensions.cpp",
                "darwin/kernel_info.cpp",
                "darwin/kernel_panics.cpp",
                "darwin/keychain_acl.cpp",
                "darwin/keychain_items.cpp",
                "darwin/keychain_utils.cpp",
                "darwin/launchd.cpp",
                "darwin/managed_policies.cpp",
                "darwin/mdfind.mm",
                "darwin/mounts.cpp",
                "darwin/nfs_shares.cpp",
                "darwin/nvram.cpp",
                "darwin/os_version.cpp",
                "darwin/packages.mm",
                "darwin/pci_devices.cpp",
                "darwin/preferences.cpp",
                "darwin/process_open_descriptors.cpp",
                "darwin/processes.cpp",
                "darwin/quicklook_cache.cpp",
                "darwin/running_apps.mm",
                "darwin/sandboxes.cpp",
                "darwin/screenlock.mm",
                "darwin/shared_folders.mm",
                "darwin/sharing_preferences.cpp",
                "darwin/signature.mm",
                "darwin/sip_config.cpp",
                "darwin/smbios_tables.cpp",
                "darwin/smc_keys.cpp",
                "darwin/startup_items.cpp",
                "darwin/sysctl_utils.cpp",
                "darwin/system_info.cpp",
                "darwin/time_machine.cpp",
                "darwin/usb_devices.cpp",
                "darwin/user_groups.mm",
                "darwin/virtual_memory_info.cpp",
                "darwin/xprotect.cpp",
            ],
        ),
        (
            FREEBSD,
            [
                "freebsd/fbsd_kmods.cpp",
                "freebsd/groups.cpp",
                "freebsd/mounts.cpp",
                "freebsd/os_version.cpp",
                "freebsd/pkg_packages.cpp",
                "freebsd/process_open_files.cpp",
                "freebsd/processes.cpp",
                "freebsd/procstat.cpp",
                "freebsd/sysctl_utils.cpp",
                "freebsd/users.cpp",
                "freenux/cpu_time.cpp",
            ],
        ),
        (
            WINDOWS,
            [
                "windows/appcompat_shims.cpp",
                "windows/authenticode.cpp",
                "windows/autoexec.cpp",
                "windows/bitlocker_info.cpp",
                "windows/certificates.cpp",
                "windows/chocolatey_packages.cpp",
                "windows/cpu_info.cpp",
                "windows/default_environment.cpp",
                "windows/disk_info.cpp",
                "windows/drivers.cpp",
                "windows/groups.cpp",
                "windows/hvci_status.cpp",
                "windows/ie_extensions.cpp",
                "windows/intel_me.cpp",
                "windows/kernel_info.cpp",
                "windows/kva_speculative_info.cpp",
                "windows/logged_in_users.cpp",
                "windows/logical_drives.cpp",
                "windows/logon_sessions.cpp",
                "windows/ntdomains.cpp",
                "windows/ntfs_acl_permissions.cpp",
                "windows/objects.cpp",
                "windows/os_version.cpp",
                "windows/patches.cpp",
                "windows/physical_disk_performance.cpp",
                "windows/pipes.cpp",
                "windows/processes.cpp",
                "windows/programs.cpp",
                "windows/registry.cpp",
                "windows/scheduled_tasks.cpp",
                "windows/services.cpp",
                "windows/shared_resources.cpp",
                "windows/smbios_tables.cpp",
                "windows/startup_items.cpp",
                "windows/system_info.cpp",
                "windows/user_groups.cpp",
                "windows/users.cpp",
                "windows/video_info.cpp",
                "windows/windows_crashes.cpp",
                "windows/windows_optional_features.cpp",
                "windows/wmi_bios_info.cpp",
                "windows/wmi_cli_event_consumers.cpp",
                "windows/wmi_event_filters.cpp",
                "windows/wmi_filter_consumer_binding.cpp",
                "windows/wmi_script_event_consumers.cpp",
                "windows/windows_security_products.cpp",
                "windows/windows_security_center.cpp",
            ],
        ),
    ],
    tests = [
        osquery_target("osquery/tables/system/tests:linux_tests"),
        osquery_target("osquery/tables/system/tests:posix_tests"),
        osquery_target("osquery/tables/system/tests:system_tables_tests"),
        osquery_target("osquery/tables/system/tests:darwin_tests"),
        osquery_target("osquery/tables/system/tests:windows_tests"),
    ],
    visibility = ["PUBLIC"],
    deps = [
        osquery_target("osquery:headers"),
        osquery_target("osquery/core:core"),
        osquery_target("osquery/events:events"),
        osquery_target("osquery/filesystem:osquery_filesystem"),
        osquery_target("osquery/hashing:hashing"),
        osquery_target("osquery/logger:logger"),
        osquery_target("osquery/process:process"),
        osquery_target("osquery/sql:sql"),
        osquery_target("osquery/utils:utils"),
        osquery_target("osquery/utils/conversions:conversions"),
        osquery_target("osquery/utils/expected:expected"),
        osquery_target("osquery/utils/system:env"),
        osquery_target("osquery/utils/system:filepath"),
        osquery_target("osquery/utils/system:system_utils"),
        osquery_target("osquery/utils/system:time"),
        osquery_target("osquery/utils/system:uptime"),
        osquery_target("specs:tables_processes_row"),
        osquery_tp_target("augeas"),
        osquery_tp_target("boost"),
        osquery_tp_target("libmagic"),
        osquery_tp_target("libxml2"),
    ],
)
